package com.wsq.gesture

import android.os.Bundle
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.GestureDetectorCompat


class MainActivity : AppCompatActivity() {

    private var mDetector: GestureDetectorCompat? = null
    private val DEBUG_TAG = "Gestures"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mDetector = GestureDetectorCompat(this, object :GestureDetector.OnGestureListener{
            override fun onDown(p0: MotionEvent?): Boolean {
                Log.d(DEBUG_TAG,"onDown: " + p0.toString())
                return true
            }

            override fun onShowPress(p0: MotionEvent?) {
                Log.d(DEBUG_TAG,"onShowPress: " + p0.toString())
            }

            override fun onSingleTapUp(p0: MotionEvent?): Boolean {
                Log.d(DEBUG_TAG, "onSingleTapUp: " + p0.toString())
                return true
            }

            override fun onScroll(
                p0: MotionEvent?,
                p1: MotionEvent?,
                p2: Float,
                p3: Float
            ): Boolean {
                Log.d(DEBUG_TAG, "onScroll: " + p0.toString() + p1.toString())
                return true
            }

            override fun onLongPress(p0: MotionEvent?) {
                Log.d(DEBUG_TAG, "onLongPress: " + p0.toString())
            }

            override fun onFling(
                p0: MotionEvent?,
                p1: MotionEvent?,
                p2: Float,
                p3: Float
            ): Boolean {
                Log.d(DEBUG_TAG, "onFling: " + p0.toString() + p1.toString())
                return true
            }
        })
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        //必须在这里获取触屏事件，否则无法回调手势信息
//        if (mDetector?.onTouchEvent(event) == true) {
//            return true
//        }

        return super.onTouchEvent(event)
    }
}